/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  MessageApi.java   
 * @Package com.jiyi.service.api   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-8-4 下午1:49:16   
 * @version V1.0     
 */
package com.jiyi.service.api.message;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.bean.message.Message;
import com.jiyi.bean.zone.JiYouXiuBean;
import com.jiyi.bean.zone.MyMessageBean;
import com.jiyi.bean.zone.ZoneHuoDongBean;
import com.jiyi.common.VolleyRequest;
import com.jiyi.event.MessageListEvent;
import com.jiyi.event.RefreshHintEvent;
import com.jiyi.service.MessageService;
import com.jiyi.service.api.BaseUrl;
import com.jiyi.service.api.BaseWebServiceAPI;
import com.jiyi.service.api.PostType;
import com.jiyi.service.impl.MessageServiceImpl;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.zhy.http.okhttp.callback.StringCallback;

/**
 * @ClassName: MessageApi
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-8-4 下午1:49:16
 * 
 */
public class MessageApi {

	public static void responseRecMsg(Map param) {
		BaseWebServiceAPI.getInstance().reqeustUrl(
				BaseUrl.ROJECT + "Message/messageRec", param, PostType.POST,
				new StringCallback() {

					@Override
					public void onResponse(String result, int arg1) {

						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								//执行成功
							} else {
								//执行失败
								// ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}

					}

					@Override
					public void onError(Call arg0, Exception arg1, int arg2) {
						Log.e("responseRecMsg error: ", arg1.getMessage());
					}
				});
	}
	
	public static void getNoRecMsgList(Map param)
	{
		BaseWebServiceAPI.getInstance().reqeustUrl(
				BaseUrl.ROJECT + "Message/no_rec_messageList", param, PostType.POST,
				new StringCallback() {

					@Override
					public void onResponse(String result, int arg1) {

						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								/** 执行成功 **/
								//初始化消息服务
								MessageService mMessageService = new MessageServiceImpl();
								//判断是否有数据更新
								boolean isUpdate = false;
								//更新的消息id
								StringBuilder idBuilder = new StringBuilder();
								String data = obj.getString("data");
								//获取未接收的消息数据
								List<MyMessageBean> temp = new Gson().fromJson(data,
										new TypeToken<List<MyMessageBean>>() {
								}.getType());
								for (int i = 0; i < temp.size(); i++) {
									final MyMessageBean myMessageBean = temp.get(i);
									if(mMessageService.isNoExist(myMessageBean.getId()))
									{
										Message message = new Message();
										message.setAccount(UserInfoUtil.getCurrentUserAccount());
										message.setBelongType(Integer.parseInt(myMessageBean.getAttr_type()));
										message.setCreateDate(myMessageBean.getCreate_time());
										message.setIsDel(false);
										message.setIsHandle(false);
										message.setIsReaded(false);
										message.setJyid(myMessageBean.getId());
										message.setMsgContent(myMessageBean.getContent());
										message.setMsgTitle(myMessageBean.getTitle());
										message.setMsgIcon(myMessageBean.getUser_logo());
										message.setMsgType(Integer.parseInt(myMessageBean.getType()));
										message.setReceive_user_id(myMessageBean.getReceive_user_id());
										message.setSend_user_id(myMessageBean.getSend_user_id());
										message.setMatchId(myMessageBean.getRemark());
										boolean isSuccess = message.save();
										if (isSuccess) {
											isUpdate = true;
											idBuilder.append(myMessageBean.getId());
											idBuilder.append(",");
											Log.e("getNoRecMsgList  " , "保存消息"+myMessageBean.getId());
										}
										
									}
									
									Map<String, String> paraMap = new HashMap<String, String>();
									paraMap.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
									paraMap.put("id", myMessageBean.getId());
									BaseWebServiceAPI.getInstance().reqeustUrl(BaseUrl.ROJECT + "Message/messageRec", paraMap, PostType.POST, new StringCallback() {
										
										@Override
										public void onResponse(String arg0, int arg1) {
											Log.e("messageRec  " , "返回接收消息"+myMessageBean.getId());
										}
										
										@Override
										public void onError(Call arg0, Exception arg1, int arg2) {
											// TODO Auto-generated method stub
											
										}
									});
								}
								
								//如果有数据更新 则发送更新通知
								if (isUpdate) {
									Log.e("ids  " , idBuilder.toString());
									EventBus.getDefault().post(new RefreshHintEvent(RefreshHintEvent.MIME));
									EventBus.getDefault().post(new MessageListEvent(MessageListEvent.MY_MESSAGE));
								}
								
							} else {
								//执行失败
								// ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}

					}

					@Override
					public void onError(Call arg0, Exception arg1, int arg2) {
						Log.e("responseRecMsg error: ", "返回已接收到的消息异常");
					}
				});
	}
}
